#include "PcDuinoManager.h"
#include "GPIOManager.h"

int initCard(PcDuino *card)
{
	if( card != NULL )
	{
		int i = 0;
		
		for( i = 0; i < 18; ++i )
		{
			if( (card->gpioModeFiles[i] = openGpioModeFile(i)) == NULL )
				return -1;
			if( (card->gpioPinFiles[i][0] = openGpioPinFile(i, "r")) == NULL )
				return -1;
			if( (card->gpioPinFiles[i][1] = openGpioPinFile(i, "w")) == NULL )
				return -1;
		}
		return 0;
	}
	return -1;
}

int stopCard(PcDuino *card)
{
	if( card != NULL )
	{
		int i = 0;
		
		for( i = 0; i < 18; ++i )
		{
			fclose( card->gpioModeFiles[i] );
			fclose( card->gpioPinFiles[i][0] );
			fclose( card->gpioPinFiles[i][1] );
		}
		return 0;
	}
	
	return -1;
}

int setModePin(PcDuino *card, int numPin, char* mode)
{
	return pinMode(card->gpioModeFiles[numPin], mode);
}

int setValuePin(PcDuino *card, int numPin, char* mode)
{
	return pinWrite(card->gpioPinFiles[numPin][1], mode);
}

int getValuePin(PcDuino *card, int numPin)
{
	return pinRead(card->gpioPinFiles[numPin][0]);
}
